<?php 
add_action( 'add_meta_boxes', 'banban_meta_boxes' );
function banban_meta_boxes() {
	add_meta_box( 'banban-advisory', __('番剧', 'banban'), 'banban_meta_box', 'specialt', 'normal', 'high' );
}

function banban_meta_box($post) {

	global $post, $wpdb, $thepostid;


    $sp_alias =  get_post_meta($post->ID, 'sp_alias', true) ;

	$person =  get_post_meta($post->ID, 'person', true);
	$areaid =  get_post_meta($post->ID, 'areaid', true);
	$typeid =  get_post_meta($post->ID, 'typeid', true);
	
	$country = array(
		"0" => array(name => "中国大陆", vale => "1"),
		"1" => array(name => "日本",    vale => "2"),
		"2" => array(name => "美国",    vale => "3"),
		"3" => array(name => "英国",    vale => "4"),
		"4" => array(name => "加拿大",   vale => "5"),
		"5" => array(name => "中国香港",  vale => "6"),
		"6" => array(name => "中国台湾",  vale => "7"),
		"7" => array(name => "韩国",     vale => "8"),
		"8" => array(name => "法国",     vale => "9"),
		"9" => array(name => "泰国",     vale => "10"),
		"15" => array(name => "马来西亚", vale => "11"),
		"10" => array(name => "新加坡",   vale => "12"),
		"11" => array(name => "西班牙",   vale => "13"),
		"12" => array(name => "俄罗斯",   vale => "14"),
		"13" => array(name => "德国",     vale => "15"),
		"14" => array(name => "其他",     vale => "16"),
	);
	?>

	<div class="panels">
		<div id="order_data" class="panel">
			<p class="form-field"><label>同义词：</label><input type="text" id="sp_alias" name="sp_alias" style="width:300px;" value="<?php echo esc_attr( $sp_alias ); ?>"><i>用半角逗号(,)分隔</i></p>
			<p class="form-field"><label>声&nbsp;&nbsp;&nbsp;&nbsp;优：</label><input type="text" name="person" value="<?php echo esc_attr( $person ); ?>" style="width:300px;"></p>
            <p class="form-field"><label>地区</label>
			<select name="areaid">
			<?php
                foreach ( $country as $v ) :
				print_r( $v);
                    echo '<option value="'.esc_attr($v['vale']).'" ';
						if ($v['vale']==$areaid) echo 'selected="selected"';
						echo '>'. $v['name'].'</option>';
                endforeach;
            ?>
            </select>
            </p>

			<p class="form-field"><label >版本</label>
            <select name="typeid">
            	<option selected="selected" value="0">其他</option>
                <option value="1" <?php if($typeid == 1){ echo 'selected="selected"';}?>>TV版</option>
                <option value="2" <?php if($typeid == 2){ echo 'selected="selected"';}?>>OVA版</option>
                <option value="3" <?php if($typeid == 3){ echo 'selected="selected"';}?>>剧场版</option>
                <option value="4" <?php if($typeid == 4){ echo 'selected="selected"';}?>>OAD版</option>  
             </select>
            </p>

		 </div>

	</div>
	<?php

}


add_action( 'save_post', 'banban_meta_boxes_save', 1, 2 );
add_action('banban_process_specialt_meta', 'banban_process_specialt_meta', 1, 2);

function banban_meta_boxes_save( $post_id, $post ) {
	global $wpdb;
//echo "<pre>";print_r($_POST);echo "</pre>";exit;
	if ( !$_POST ) return $post_id;
	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
	if ( !current_user_can( 'edit_post', $post_id )) return $post_id;
	if ( $post->post_type != 'specialt' ) return $post_id;

	do_action( 'banban_process_'.$post->post_type.'_meta', $post_id, $post );
}

function banban_process_specialt_meta($post_id, $post){
	//echo $_POST['sp_alias'].$post_id;exit;
	update_post_meta($post_id, 'sp_alias', $_POST['sp_alias']);
	update_post_meta($post_id, 'person', $_POST['person']);
	update_post_meta($post_id, 'areaid', $_POST['areaid']);
	update_post_meta($post_id, 'typeid', $_POST['typeid']);
}
?>